package com.md.api.controller;

import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.utils.IpUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.HttpResult;
import com.md.api.entity.ShoppingCart;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {


	@Resource
	ShoppingCartService cartSevice;



	//获取购物车产品总价及积分
	@RequestMapping("/shoppingCartTrade")
	@ResponseBody
	public HttpResult shoppingCartTrade(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (CollectionUtils.isEmpty(qto.getCartIds())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.shoppingCartTrade(request,qto);
	}


	//商城购物车下单
	@NoRepeatSubmit
	@RequestMapping("/orderShoppingCart")
	@ResponseBody
	public HttpResult orderShoppingCart(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		try {
			if (StringUtils.isEmpty(qto.getReceivingAddrId())) {
				return HttpResult.PARAM_ERROR;
			}
			return cartSevice.orderShoppingCart(request, qto);
		}catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("下单失败："+e.getMessage());
		}
	}



	//加入商城购物车
	@NoRepeatSubmit
	@RequestMapping("/addShoppingCart")
	@ResponseBody
	public HttpResult addShoppingCart(HttpServletRequest request,@RequestBody ShoppingCart shoppingCart) {
		if (null==shoppingCart.getNumCount()|| StringUtils.isEmpty(shoppingCart.getSpuId())||StringUtils.isEmpty(shoppingCart.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.addShoppingCart(request,shoppingCart);
	}

	//我的购物车
	@RequestMapping("/myShoppingCart")
	@ResponseBody
	public HttpResult myShoppingCart(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		return cartSevice.myShoppingCart(request,qto);
	}

	//移除购物车
	@NoRepeatSubmit
	@RequestMapping("/removeShoppingCart")
	@ResponseBody
	public HttpResult removeShoppingCart(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getCartId())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.removeShoppingCart(request,qto);
	}

	@NoRepeatSubmit
	@RequestMapping("/resetProductCount")
	@ResponseBody
	public HttpResult resetProductCount(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getCartId())||null==qto.getCount()||qto.getCount()<1){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.resetProductCount(request,qto);
	}

	@RequestMapping("/shoppingNotify")
	public String shoppingNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String ip = IpUtil.getRealIp(request);
		log.info("回调请求方ip："+ip);
		log.info("shoppingNotify回调啦："+request.toString());
		return cartSevice.shoppingNotify(request,true);
	}

	@RequestMapping("/backShoppingNotify")
	public String backShoppingNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String ip = IpUtil.getRealIp(request);
		log.info("回调请求方ip："+ip);
		log.info("shoppingNotify回调啦："+request.toString());
		return cartSevice.backShoppingNotify(request);
	}
	@NoRepeatSubmit
	@RequestMapping("/refundProduct")
	@ResponseBody
	public HttpResult refundProduct(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {

		if (StringUtils.isEmpty(qto.getOrderNo())||StringUtils.isEmpty(qto.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.refundProduct(request,qto);
	}

	@NoRepeatSubmit
	@RequestMapping("/cancelRefund")
	@ResponseBody
	public HttpResult cancelRefund(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getOrderNo())||StringUtils.isEmpty(qto.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.cancelRefund(request,qto);
	}

	//填写退货信息
	@NoRepeatSubmit
	@RequestMapping("/fillReturnInfo")
	@ResponseBody
	public HttpResult fillReturnInfo(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		if (StringUtils.isEmpty(qto.getOrderNo())||StringUtils.isEmpty(qto.getSkuId())
				||StringUtils.isEmpty(qto.getShippingCode())||StringUtils.isEmpty(qto.getShippingName())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.fillReturnInfo(request,qto);
	}

	@NoRepeatSubmit
	@RequestMapping("/changeProduct")
	@ResponseBody
	public HttpResult changeProduct(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getOrderNo())||StringUtils.isEmpty(qto.getReceivingAddrId())||
				null==qto.getNumCount()||StringUtils.isEmpty(qto.getSkuId())||StringUtils.isEmpty(qto.getNewSkuId())) {
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.changeProduct(request,qto);
	}


	@NoRepeatSubmit
	@RequestMapping("/changeDetail")
	@ResponseBody
	public HttpResult changeDetail(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getOrderNo())||StringUtils.isEmpty(qto.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.changeDetail(request,qto);
	}

	@NoRepeatSubmit
	@RequestMapping("/cancelChange")
	@ResponseBody
	public HttpResult cancelChange(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getOrderNo())||StringUtils.isEmpty(qto.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.cancelChange(request,qto);
	}

//	//填写换货信息
//	@NoRepeatSubmit
//	@RequestMapping("/fillChangeInfo")
//	@ResponseBody
//	public HttpResult fillChangeInfo(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
//		if (StringUtils.isEmpty(qto.getOrderNo())||StringUtils.isEmpty(qto.getSkuId())
//				||StringUtils.isEmpty(qto.getShippingCode())||StringUtils.isEmpty(qto.getShippingName())){
//			return HttpResult.PARAM_ERROR;
//		}
//		return cartSevice.fillChangeInfo(request,qto);
//	}

	//根据快递单号查询物流信息
	@RequestMapping(value = "/logistics")
	public HttpResult logistics(@RequestBody ComposeQueryDto queryDto) {
		return cartSevice.logistics(queryDto);
	}
}
